package uba.ing2.subite.services.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import uba.ing2.subite.domain.Account;
import uba.ing2.subite.services.AccountService;
import uba.ing2.subite.services.Clock;

public class AccountServiceImpl implements AccountService {
	private int lastAccountIdentifier = 1;
	private Clock clock;
	private Map<Serializable, Account> accounts;
	
	public AccountServiceImpl(Clock clock) {
		this.clock = clock;
		accounts = new HashMap<Serializable, Account>();
	}

	@Override
	public Account createAccount(Number initialBalance) {
		Account newAccount = Account.createWithInitialBalance("000" + (lastAccountIdentifier ++), clock.currentTime(), initialBalance);
		accounts.put(newAccount.identifier(), newAccount);
		return newAccount;
	}

	@Override
	public Account accountIdentifiedBy(Serializable identifier) {
		return accounts.get(identifier);
	}

	@Override
	public Number balanceOnCurrentTimeForAccountIdentifiedBy(Serializable accountIdentifier) {
		Account account = accountIdentifiedBy(accountIdentifier);
		return account.balanceOn(clock.currentTime());
	}
}
